package com.bilibili.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.bilibili.dao.DanmakuDao;
import com.bilibili.dao.UserDao;
import com.bilibili.dao.VideoDao;
import com.bilibili.dao.impl.DanmakuDaoImpl;
import com.bilibili.dao.impl.UserDaoImpl;
import com.bilibili.dao.impl.VideoDaoImpl;
import com.bilibili.dto.DanmakuInfo;
import com.bilibili.entity.Danmaku;
import com.bilibili.entity.Page;
import com.bilibili.entity.Video;
import com.bilibili.service.DanmakuService;

public class DanmakuServiceImpl implements DanmakuService {
	private DanmakuDao danmakuDao = new DanmakuDaoImpl();
	private VideoDao videoDao = new VideoDaoImpl();
	UserDao ud = new UserDaoImpl();
	VideoDao vd = new VideoDaoImpl();
	@Override
	public Page<Danmaku> selectDanmakuByPage(int upId, int pageNum, int pageSize) {
		return danmakuDao.selectByUpIdWithPage(upId, pageNum, pageSize);
	}

	@Override
	public Page<Danmaku> selectDanmakuByPageWithFilters(int upId, int pageNum, int pageSize, String keyword,
			String timeRange, String videoTitle) {
		// 构建基础查询条件
		String whereClause = "WHERE up_id = ?";
		List<Object> params = new ArrayList<>();
		params.add(upId);

		// 添加关键词筛选条件
		if (!keyword.isEmpty()) {
			whereClause += " AND content LIKE ?";
			params.add("%" + keyword + "%");
		}

		// 添加时间范围筛选条件
		if (!timeRange.isEmpty()) {
			whereClause += " AND create_time >= ?";
			// 计算时间范围
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DAY_OF_MONTH, -Integer.parseInt(timeRange));
			params.add(new Timestamp(cal.getTimeInMillis()));
		}

		// 视频标题筛选（在结果中处理）
		if (!videoTitle.isEmpty()) {
			// 先查询所有符合条件的弹幕
			// 再通过视频标题过滤
		}

		// 计算总记录数
		int totalRows = danmakuDao.selectTotalRowsByUpIdWithFilters(upId, whereClause, params.toArray());

		// 构建分页查询SQL
		String sql = "SELECT * FROM danmaku " + whereClause + " LIMIT ?, ?";
		params.add((pageNum - 1) * pageSize);
		params.add(pageSize);

		// 查询弹幕数据
		Page<Danmaku> danmakuPage = danmakuDao.selectByUpIdWithPageAndFilters(sql, params.toArray());
		List<Danmaku> danmakuList = danmakuPage.getData();

		// 批量获取视频标题
		if (!danmakuList.isEmpty()) {
			// 收集所有视频ID
			List<Integer> videoIds = new ArrayList<>();
			for (Danmaku danmaku : danmakuList) {
				videoIds.add(danmaku.getVideoId());
			}

			// 批量查询视频
			List<Video> videos = videoDao.selectVideosByIds(videoIds);

			// 为弹幕设置视频标题
			for (Danmaku danmaku : danmakuList) {
				String videoTitleText = "未指定视频";
				for (Video video : videos) {
					if (video.getVideoId() == danmaku.getVideoId()) {
						videoTitleText = video.getTitle();
						break;
					}
				}
				danmaku.setVideoTitle(videoTitleText);
			}
		}

		return new Page<>(danmakuList, pageNum, pageSize, totalRows);
	}

	@Override
	public int getDanmakuCount(int upId) {
		return danmakuDao.selectCountByUpId(upId);
	}

	@Override
	public int getBlockedDanmakuCount(int upId) {
		return danmakuDao.selectCountByUpIdD(upId);
	}

	@Override
	public boolean updateDanmakuStatus(int danmakuId, int status) {
		int affectedRows = danmakuDao.updateStatus(danmakuId, status);
		return affectedRows > 0;
	}

	public List<Danmaku> getDanmakusByVideoId(int videoId) {
		List<Danmaku> danmakus = danmakuDao.selectDanmakuByVideoId(videoId);

		// 如果弹幕中没有视频标题，从视频表获取并设置
		if (!danmakus.isEmpty()) {
			Video video = videoDao.selectVideoById(videoId);
			String videoTitle = video != null ? video.getTitle() : "未知视频";

			for (Danmaku danmaku : danmakus) {
				danmaku.setVideoTitle(videoTitle);
			}
		}

		return danmakus;
	}
	DanmakuDao dd = new DanmakuDaoImpl();
	@Override
	public int selectAllDanmakuNum() {
		// TODO Auto-generated method stub
		return dd.selectAllDanmakuNum();
	}

	
	@Override
	public Page<DanmakuInfo> selectAllDanmakuB(String content, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		List<DanmakuInfo> danmakuList = dd.selectAllDanmakuB(content, pageNum, pageSize);
		for (DanmakuInfo d : danmakuList) {
			d.setUserName(ud.selectUserNameById(d.getUserId()));
			d.setVideoTitle(vd.selectVideoTitleStateByVideoId(d.getVideoId()));
		}
		int totalRows = dd.selectAllDanmakuBNum(content);
		Page<DanmakuInfo> danmakuPage = new Page<DanmakuInfo>(danmakuList,pageNum,pageSize,totalRows);
		return danmakuPage;
	}
	
	
	@Override
	public boolean deleteDanmakuB(int danmakuId) {
		// TODO Auto-generated method stub
		return dd.deleteDanmakuB(danmakuId) == 1;
	}
}